package com.dongdong.android.tangramdemo.utils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by dongdongzheng on 2018/11/16.
 */

public class Httputil {


    public static void getdata(final String url, final VVout vout) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();

                try {
                    Response response = client.newCall(request).execute();
                    if (response != null && response.isSuccessful()) {
                        if (response.body() != null) {
                            String string = response.body().string();
                            final PreviewData previewData = new Gson().fromJson(string, PreviewData.class);
                            if (previewData != null) {
                                vout.out(previewData);
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public interface VVout {
        void out(PreviewData data);
    }

}
